package com.woniuedu.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;


/** 
 * author: 承雍
 * Date: 2022/4/25 18:37
 * define：类注释
 * Description:
 */

@Component
@Primary
public class MetaHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        this.setFieldValByName("gmtCreate",localDateTime, metaObject);
        this.setFieldValByName("gmtModified", localDateTime, metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        System.out.println("localDateTime"+localDateTime);
        this.setFieldValByName("gmtModified", localDateTime, metaObject);
    }
}